﻿using System;
using System.Drawing;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmdmnguonns.
	/// </summary>
	public class frmdmnguonns : System.Windows.Forms.Form
	{
		#region khoitao form
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.CheckBox chsd;
		private System.Windows.Forms.TextBox mans;
		private System.Windows.Forms.TextBox tenns;
		private System.Windows.Forms.TextBox mada;
		private System.Windows.Forms.TextBox tenda;
		private System.Windows.Forms.Button btn_in;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butKetthuc;
		#endregion 

		AccessData a=new AccessData();
		int i_user=0;
		long l_id=0;
		string sql="";
		DataRow r;
		bool bnew=false;
		DataSet ds=new DataSet();
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmdmnguonns(AccessData acc, int i_userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			i_user=i_userid;
			a=acc;

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmdmnguonns));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.chsd = new System.Windows.Forms.CheckBox();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.mans = new System.Windows.Forms.TextBox();
            this.tenns = new System.Windows.Forms.TextBox();
            this.mada = new System.Windows.Forms.TextBox();
            this.tenda = new System.Windows.Forms.TextBox();
            this.btn_in = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butMoi = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(2, 226);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(96, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Mã Nguồn NS :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(188, 226);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(96, 23);
            this.label2.TabIndex = 0;
            this.label2.Text = "Tên nguồn NS :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(24, 249);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(74, 23);
            this.label3.TabIndex = 0;
            this.label3.Text = "Mã dự án :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(209, 249);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(72, 23);
            this.label4.TabIndex = 0;
            this.label4.Text = "Tên dự án :";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // chsd
            // 
            this.chsd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.chsd.Enabled = false;
            this.chsd.Location = new System.Drawing.Point(562, 251);
            this.chsd.Name = "chsd";
            this.chsd.Size = new System.Drawing.Size(107, 24);
            this.chsd.TabIndex = 5;
            this.chsd.Text = "Không sử dụng";
            this.chsd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // dataGrid1
            // 
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(8, -14);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(661, 224);
            this.dataGrid1.TabIndex = 12;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // mans
            // 
            this.mans.Enabled = false;
            this.mans.Location = new System.Drawing.Point(82, 228);
            this.mans.Name = "mans";
            this.mans.Size = new System.Drawing.Size(100, 20);
            this.mans.TabIndex = 1;
            this.mans.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // tenns
            // 
            this.tenns.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tenns.Enabled = false;
            this.tenns.Location = new System.Drawing.Point(275, 229);
            this.tenns.Name = "tenns";
            this.tenns.Size = new System.Drawing.Size(390, 20);
            this.tenns.TabIndex = 2;
            this.tenns.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // mada
            // 
            this.mada.Enabled = false;
            this.mada.Location = new System.Drawing.Point(82, 251);
            this.mada.Name = "mada";
            this.mada.Size = new System.Drawing.Size(100, 20);
            this.mada.TabIndex = 3;
            this.mada.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // tenda
            // 
            this.tenda.Enabled = false;
            this.tenda.Location = new System.Drawing.Point(275, 251);
            this.tenda.Name = "tenda";
            this.tenda.Size = new System.Drawing.Size(279, 20);
            this.tenda.TabIndex = 4;
            this.tenda.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // btn_in
            // 
            this.btn_in.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btn_in.Image = ((System.Drawing.Image)(resources.GetObject("btn_in.Image")));
            this.btn_in.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_in.Location = new System.Drawing.Point(424, 273);
            this.btn_in.Name = "btn_in";
            this.btn_in.Size = new System.Drawing.Size(60, 25);
            this.btn_in.TabIndex = 11;
            this.btn_in.Text = "      &In";
            // 
            // butHuy
            // 
            this.butHuy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butHuy.Image = ((System.Drawing.Image)(resources.GetObject("butHuy.Image")));
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(352, 273);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(60, 25);
            this.butHuy.TabIndex = 10;
            this.butHuy.Text = "     &Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butBoqua.Enabled = false;
            this.butBoqua.Image = ((System.Drawing.Image)(resources.GetObject("butBoqua.Image")));
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(288, 273);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(60, 25);
            this.butBoqua.TabIndex = 7;
            this.butBoqua.Text = "&Bỏ qua";
            this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butLuu
            // 
            this.butLuu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butLuu.Enabled = false;
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(224, 273);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(60, 25);
            this.butLuu.TabIndex = 6;
            this.butLuu.Text = "    &Lưu";
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            this.butLuu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mans_KeyDown);
            // 
            // butSua
            // 
            this.butSua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butSua.Image = ((System.Drawing.Image)(resources.GetObject("butSua.Image")));
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(160, 273);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(60, 25);
            this.butSua.TabIndex = 9;
            this.butSua.Text = "     &Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butMoi
            // 
            this.butMoi.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butMoi.Image = ((System.Drawing.Image)(resources.GetObject("butMoi.Image")));
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(94, 273);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(60, 25);
            this.butMoi.TabIndex = 8;
            this.butMoi.Text = "    &Mới";
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(488, 273);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(68, 25);
            this.butKetthuc.TabIndex = 12;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // frmdmnguonns
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(677, 319);
            this.Controls.Add(this.btn_in);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butBoqua);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butMoi);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.tenda);
            this.Controls.Add(this.mada);
            this.Controls.Add(this.tenns);
            this.Controls.Add(this.mans);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.chsd);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "frmdmnguonns";
            this.Text = "Danh mục nguồn ngân sách nhà nước";
            this.Load += new System.EventHandler(this.frmdmnguonns_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmdmnguonns_Load(object sender, System.EventArgs e)
		{
			sql=" select * from a_dmngns where sudung=1 order by id";
			ds=a.get_data(sql);
			dataGrid1.DataSource=ds.Tables[0];
			AddGridTableStyle();

		}
		public void loadgrid()
		{
			sql=" select * from a_dmngns where sudung=1 order by id";
			ds=a.get_data(sql);
			dataGrid1.DataSource=ds.Tables[0];
		}
		public void innit(bool ena)
		{
			butMoi.Enabled=butKetthuc.Enabled=btn_in.Enabled=butSua.Enabled=!ena;
			butBoqua.Enabled=butHuy.Enabled=butLuu.Enabled= ena;
			mada.Enabled=tenda.Enabled=mans.Enabled=tenns.Enabled=chsd.Enabled=ena;
		}
		public void ref_text()
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				l_id=long.Parse(dataGrid1[i,0].ToString());
				r=a.getrowbyid(ds.Tables[0],"id="+l_id+"");
				if(r!=null)
				{
					mans.Text=r["mans"].ToString();
					tenns.Text=r["tenns"].ToString();
					mada.Text=r["mada"].ToString();
					tenda.Text=r["tenda"].ToString();
					chsd.Checked=r["sudung"].ToString()=="0";
				}
			}
			catch{}
		}
		private void AddGridTableStyle()
		{			
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();			
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "mans";
			TextCol.HeaderText = "Mã nguồn NS";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tenns";
			TextCol.HeaderText = "Tên nguồn NS";
			TextCol.Width = 150;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "mada";
			TextCol.HeaderText = "Mã dự án";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tenda";
			TextCol.HeaderText = "Tên dự án";
			TextCol.Width = dataGrid1.Width-50-150-50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);			
		}

		public long idmangns()
		{
			long idns=0;
			try
			{
				sql="select nvl(max(id),1)";
				idns=long.Parse(a.get_data(sql).Tables[0].Rows[0][0].ToString());
			}
			catch{idns=1;}
			return idns;
		}
		private bool kiemtra()
		{
			if(mans.Text=="")
			{
				MessageBox.Show("Bạn chưa nhập mã nguồn ngân sách",a.Msg);
				return false;
			}
			if(tenns.Text=="")
			{
				MessageBox.Show("Bạn chưa nhập tên nguồn ngân sách",a.Msg);
				return false;
			}
			return true;
		}
		private void butMoi_Click(object sender, System.EventArgs e)
		{
			innit(true);
			mans.Text=tenns.Text=mada.Text=tenda.Text="";
			bnew=true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if(!kiemtra()) return;
			if(bnew) l_id=idmangns()+1;
			if(!a.upddmngns(l_id,mans.Text,tenns.Text,mada.Text,tenda.Text,chsd.Checked?0:1,i_user))
			{
				MessageBox.Show("Không cập nhật được chứng từ",a.Msg);
				return;
			}
			loadgrid();
			ref_text();
			innit(false);
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			if(ds.Tables[0].Rows.Count<0)return;
			l_id=long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
			innit(true);
			mans.Focus();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			ref_text();
			innit(false);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			if(MessageBox.Show("Bạn có đồng ý hủy mã nguồn ngân sách này không !",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				a.execute_data("delete from "+a.user+".a_dmngns where id="+l_id+"");
				ref_text();
			}
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void mans_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}
	}
}
